// -----------------------------------------------------------------------------
// quick rate 
// -----------------------------------------------------------------------------
$(function(){
 $(".btnPositionType input:button").click(function(){
    var myIdarr = $(this).attr('id').split("_"); // index 1 is nickname of button 
    if(myIdarr.length != 2){ alert('error -1'); return false; } 
    var oMyDt = $(this).closest('td');
    if($(oMyDt).attr('class') == 'twoF'){ var twoFace = true; }
    else{ var twoFace = false; }
    
    var addVal = $(oMyDt).find("input.txtAddVal").val();
    if(addVal == ''){ addVal == '0'; } // return when add value is null
    if(isNaN(addVal)){ alert('error add Value'); return false; } // return when add value is not numbers
    var myDtIdArr = oMyDt.attr('id').split("_");
    if(myDtIdArr.length != 2){ alert('error -2'); return false; }
    var myRadio = $(oMyDt).find("input:radio:checked").val();
    var myBtn = myIdarr[1];
    $("input.iptRate").css("background-color", "");    
    if(_myGlobalBetStr == 'r2f'){
      var oMyTbl = $(this).closest("table").find("input.iptRate");
    }else{
      var oMyTbl = $(this).closest("table").find('tbody.tbdRate:eq(' + myRadio + ')').find("input.iptRate");
    }
    
    var sNumIdx = 4;
    if(myBtn == '999'){ // all
      $(oMyTbl).addTheVal(addVal);
    }else if(twoFace){ // two face only
      var chkNum = myBtn.substr(1,1);
      var chkType = myBtn.substr(0,1);
      var myChkStr = "";
      switch(chkType){
        case '9': myChkStr = ",56,59,4,5,6,22,44,71,"; break;
        case '8': myChkStr = ",57,60,7,8,9,62,63,64,65,66,67,68,17,18,19,23,72,"; break;
        case '7': myChkStr = ",58,61,10,11,12,45,"; break;
        default: return false; break;
      }
      $(oMyTbl).filter(function(idx,ele){
        var myN = $(ele).attr('name').split("_");
        var chkStr = ',' + myN[1] + ',';
        return ((myChkStr.indexOf(chkStr) > -1 ) && (myN[sNumIdx] == chkNum));  
      }).addTheVal(addVal);
      
    }else{
      
      switch(myBtn){
        case '11': // com
          var len = myIdarr[0].substr(0,1);
          if(len == 3){
             $(oMyTbl).filter(function(idx, ele){
                var myN = $(ele).attr('name').split("_");
                var sNum = myN[sNumIdx];
                var sNumArr = sNum.split("");
                if(sNumArr.length != 3){ return false; }
                if(sNumArr[0] == sNumArr[1]){ return false; }
                if(sNumArr[0] == sNumArr[2]){ return false; }
                if(sNumArr[1] == sNumArr[2]){ return false; }
                return true;
            }).addTheVal(addVal);
          }else if(len == 2){
            $(oMyTbl).filter(function(idx, ele){
                var myN = $(ele).attr('name').split("_");
                var sNum = myN[sNumIdx];
                var sNumArr = sNum.split("");
                if(sNumArr.length != 2){ return false; }
                if(sNumArr[0] == sNumArr[1]){ return false; }
                return true;
            }).addTheVal(addVal);
          }
          break;
        case '12': // double
          var len = myIdarr[0].substr(0,1);
          if(len == 3){
             $(oMyTbl).filter(function(idx, ele){
                var myN = $(ele).attr('name').split("_");
                var sNum = myN[sNumIdx];
                var sNumArr = sNum.split("");
                if(sNumArr.length != 3){ return false; }
                if((sNumArr[0] == sNumArr[1]) && (sNumArr[0] == sNumArr[2])){ return false; }
                if(sNumArr[0] == sNumArr[1]){ return true; }
                if(sNumArr[0] == sNumArr[2]){ return true; }
                if(sNumArr[1] == sNumArr[2]){ return true; }
                return false;
            }).addTheVal(addVal);
          }else if(len == 2){
            $(oMyTbl).filter(function(idx, ele){
                var myN = $(ele).attr('name').split("_");
                var sNum = myN[sNumIdx];
                var sNumArr = sNum.split("");
                if(sNumArr.length != 2){ return false; }
                if(sNumArr[0] == sNumArr[1]){ return true; }
                return false;
            }).addTheVal(addVal);
          }
          break;
        case '13': // ipr
          var len = myIdarr[0].substr(0,1);
          if(len == 3){
             $(oMyTbl).filter(function(idx, ele){
                var myN = $(ele).attr('name').split("_");
                var sNum = myN[sNumIdx];
                var sNumArr = sNum.split("");
                if(sNumArr.length != 3){ return false; }
                if((sNumArr[0] == sNumArr[1]) && (sNumArr[0] == sNumArr[2])){ return true; }
                return false;
            }).addTheVal(addVal);
          }
          break;
          
        case '91': // big
          $(oMyTbl).filter(function(idx,ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              return ((sNum % 10) > 4);  
           }).addTheVal(addVal);
           break;
        case '92': // small
           $(oMyTbl).filter(function(idx,ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              return ((sNum % 10) < 5);  
           }).addTheVal(addVal);
           break;
            
        case '81': // single
           $(oMyTbl).filter(function(idx,ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              return (sNum % 2 == 1);  
           }).addTheVal(addVal);
           break;
        case '82': // double
           $(oMyTbl).filter(function(idx,ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              return (sNum % 2 == 0);  
           }).addTheVal(addVal);
           break;
        case '71': // p  1, 2, 3, 5, 7
           $(oMyTbl).filter(function(idx,ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              if(sNum == 0){ return false; }
              var chkNum = sNum % 10; 
              if(chkNum == 0){ return false; }
              if(chkNum == 4){ return false; }
              if(chkNum == 6){ return false; }
              if(chkNum == 8){ return false; }
              if(chkNum == 9){ return false; }
              return true;              
           }).addTheVal(addVal);
           break;
        case '72': // c  0, 4, 6, 8, 9
          $(oMyTbl).filter(function(idx,ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              if(sNum == 0){ return true; }
              var chkNum = sNum % 10; 
              if(chkNum == 0){ return true; }
              if(chkNum == 4){ return true; }
              if(chkNum == 6){ return true; }
              if(chkNum == 8){ return true; }
              if(chkNum == 9){ return true; }
              return false;  
           }).addTheVal(addVal);
           break;
         default:
           $(oMyTbl).filter(function(idx,ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              return (sNum == myBtn);  
           }).addTheVal(addVal);
           break;
      }
    }
    
 });
 
 $('.btnmin').add('.btnplus').click(function(){
    var addVal = $("input.txtAddVal").val();
    if(addVal == ''){ addVal == '0'; } // return when add value is null
    if(isNaN(addVal)){ alert('error add Value'); return false; } // return when add value is not numbers
    $("input.iptRate").css("background-color", "");   
    var myRadio = $(this).closest("th").index();
    var oMyTbl = $("tr#myGTr").find('table:eq(' + myRadio + ')').find("input.iptRate");
    if($(this).attr('class') == 'btnmin'){ addVal = addVal * -1 ; }
    $(oMyTbl).addTheVal(addVal);
 });

});
  
$.fn.addTheVal = function(addVal){
  $(this).each(function(idx, ele){
    var myVal = parseFloat($(ele).val()) - 0;
    addVal = parseFloat(addVal) - 0;
    myVal = myVal + addVal;
    if(isNaN(addVal)){return false;}
    if(myVal < 0){ myVal = 0; }
    $(ele).val(myVal);
    if(myVal < 1){
      return $(ele).myRateOKNum().css('background-color', '#FF9291');
    }else{
      return $(ele).myRateOKNum().css('background-color', '#ffff8c');
    }
      
  });
};

// $.fn.myRateOKNum = function(){
//   var num = $(this).val();
//   var numv = num-0;
// 	var sign=(numv>=0?1:-1); // Will give 1 for num=0
// 	var numabs=numv*sign;
// 	var naint=Math.floor(numabs);
// 	var nacent=Math.round((numabs-naint)*100);
// 	if (nacent>=100) {nacent=0; naint++;}
// 	var nais=''+naint;
// 	var nacs=(nacent<10?'0':'')+nacent;
// 	if (naint+nacent==0) sign=1;
// 	var myOKNum = (sign==1?'':'-')+nais+'.'+nacs;
// 	$(this).val(myOKNum);
//   return $(this);
// };


$.fn.myRateOKNum = function(){
  var num = $(this).val();
  $(this).val(parseFloat(Math.round(num*1000))/1000);
  return $(this);
};


// sub add little btn
function btnAdd1(o, d){
  $(o).addTheVal([d]);
}